//2009/08/29 00:46:59
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

int xx[] = {0, 1, 0, -1};
int yy[] = {1, 0, -1, 0};
class SpiralWalking
{
public:
    int totalPoints(vector <string> v)
    {
        int res = 0;
        int x = 0;
        int y = 0;
        int idx = 0;
        while (1)
        {
            //res += v[x][y] - '0';
            int xtemp = x + xx[idx];
            int ytemp = y + yy[idx];
            if (xtemp<0 || xtemp>=v.size() || ytemp<0 || ytemp>=v[0].size() || v[xtemp][ytemp]=='.')
            {
                idx = (idx + 1) % 4;
                int x1 = x + xx[idx];
                int y1 = y + yy[idx];
				if (x1<0 || x1>=v.size() || y1<0 || y1>=v[0].size() || v[x1][y1]=='.') break;
                res -= v[x][y] - '0';
            }
            else
            {
                res += v[x][y] - '0';
                v[x][y] = '.';
                x = xtemp;
                y = ytemp;
            }
        }
		res += v[x][y] - '0';
        return res;
    }
};
